package com.bjsxt.userscenter.system.pojo;

import java.util.Date;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.userscenter.common.util.DateFormatUtil;

/**
 * 全国地区的pojo
 * 
 * @author wangshh
 * 
 */
public class ARegion
{
	private int id;
	private int parentId;
	private String name;
	private String pinyin;
	private String areacode;
	private String content;
	private byte leafStatus;
	private byte status ; 
	private Date createTime;
	private Date updateTime;
	private Date pubTime ; 

	// 对象的表示,关联关系
	private ARegion parentRegion;
	
	//树形分类的字符串表示
	private String treeName ; 
	private String leafStatusStr ; 
	private String statusStr ;
	
	//json的表现方式
	private JSONArray treeJSON ; 
	private JSONArray childrenListJSON ; 
	
	public int getId()
	{
		return id;
	}

	public void setId(int id)
	{
		this.id = id;
	}

	public int getParentId()
	{
		return parentId;
	}

	public void setParentId(int parentId)
	{
		this.parentId = parentId;
	}

	public String getName()
	{
		return name;
	}

	public void setName(String name)
	{
		this.name = name;
	}

	public String getPinyin()
	{
		return pinyin;
	}

	public void setPinyin(String pinyin)
	{
		this.pinyin = pinyin;
	}

	public String getAreacode()
	{
		return areacode;
	}

	public void setAreacode(String areacode)
	{
		this.areacode = areacode;
	}

	public String getContent()
	{
		return content;
	}

	public void setContent(String content)
	{
		this.content = content;
	}

	public byte getLeafStatus()
	{
		return leafStatus;
	}

	public void setLeafStatus(byte leafStatus)
	{
		this.leafStatus = leafStatus;
	}

	public byte getStatus()
	{
		return status;
	}

	public void setStatus(byte status)
	{
		this.status = status;
	}

	public Date getCreateTime()
	{
		return createTime;
	}

	public void setCreateTime(Date createTime)
	{
		this.createTime = createTime;
	}

	public Date getUpdateTime()
	{
		return updateTime;
	}

	public void setUpdateTime(Date updateTime)
	{
		this.updateTime = updateTime;
	}

	public Date getPubTime()
	{
		return pubTime;
	}

	public void setPubTime(Date pubTime)
	{
		this.pubTime = pubTime;
	}

	public ARegion getParentRegion()
	{
		return parentRegion;
	}

	public void setParentRegion(ARegion parentRegion)
	{
		this.parentRegion = parentRegion;
	}

	public String getTreeName()
	{
		return treeName;
	}

	public void setTreeName(String treeName)
	{
		this.treeName = treeName;
	}

	public JSONArray getTreeJSON()
	{
		return treeJSON;
	}

	public void setTreeJSON(JSONArray treeJSON)
	{
		this.treeJSON = treeJSON;
	}

	public JSONArray getChildrenListJSON()
	{
		return childrenListJSON;
	}

	public void setChildrenListJSON(JSONArray childrenListJSON)
	{
		this.childrenListJSON = childrenListJSON;
	}

	/**
	 * 组装成一个json
	 * @return
	 */
	public JSONObject toJSON()
	{
		DateFormatUtil dateFormatUtil = new DateFormatUtil();
		JSONObject regTempJSON = new JSONObject();
		regTempJSON.put("id", this.getId() + "");
		regTempJSON.put("parentid", this.getParentId() + "");
		regTempJSON.put("name", this.getName());
		regTempJSON.put("pinyin", this.getPinyin());
		regTempJSON.put("areacode", this.getAreacode());
		regTempJSON.put("content", this.getContent());
		regTempJSON.put("leafStatus", this.getLeafStatus() + "");
		regTempJSON.put("createtime", dateFormatUtil.format(this.getCreateTime()));
		regTempJSON.put("updatetime", dateFormatUtil.format(this.getUpdateTime()));
		regTempJSON.put("pubTime", dateFormatUtil.format(this.getPubTime()));
		regTempJSON.put("treeName", this.getTreeName());
		
		if(treeJSON != null)
		{
			//增加父节点
			regTempJSON.put("treeJSON", treeJSON);
		}
		
		if(childrenListJSON != null)
		{
			regTempJSON.put("childrenListJSON", childrenListJSON);
		}
		
		return regTempJSON ;
	}
	
	/**
	 * 解析一个json
	 * @param souJSON
	 * @return
	 */
	public ARegion parseJson(JSONObject souJSON)
	{
		DateFormatUtil dateFormatUtil = new DateFormatUtil();
		ARegion region = new ARegion();
		region.setId(Integer.valueOf(souJSON.get("id") + ""));
		if(souJSON.get("parentid") != null && !"null".equalsIgnoreCase(souJSON.get("parentid") + ""))
		{
			region.setParentId(Integer.valueOf(souJSON.get("parentid") + ""));
		}
		region.setName(souJSON.get("name") + "");
		region.setPinyin(souJSON.get("pinyin") + "");
		region.setAreacode(souJSON.get("areacode") + "");
		region.setLeafStatus(Byte.valueOf(souJSON.get("leafStatus") + ""));
		region.setCreateTime(dateFormatUtil.parse(souJSON.get("createtime") + ""));
		region.setUpdateTime(dateFormatUtil.parse(souJSON.get("updatetime") + ""));
		region.setPubTime(dateFormatUtil.parse(souJSON.get("pubTime") + ""));
		region.setAreacode(souJSON.get("treeName") + "");
		
		region.setTreeJSON((JSONArray) souJSON.get("treeJSON"));
		region.setChildrenListJSON((JSONArray) souJSON.get("childrenListJSON"));
		return region ; 
	}

	@Override
	public String toString()
	{
		return "ARegion [id=" + id + ", parentid=" + parentId + ", name=" + name + ", pinyin=" + pinyin + ", areacode="
				+ areacode + ", content=" + content + ", leafStatus=" + leafStatus + ", status=" + status
				+ ", createTime=" + createTime + ", updateTime=" + updateTime + ", pubTime=" + pubTime
				+ ", parentRegion=" + parentRegion + "]";
	}

	public String getLeafStatusStr()
	{
		// 根据状态值获取字符串描述
		ARegionEnum[] regionEnums = ARegionEnum.values();
		for (int i = 0; i < regionEnums.length; i++)
		{
			ARegionEnum regionEnum = regionEnums[i];
			if (regionEnum.toString().startsWith("LEAFSTATUS_"))
			{
				// 表示是状态的标识
				if (regionEnum.getStatus() == this.getLeafStatus())
				{
					this.leafStatusStr = regionEnum.getName();
					break;
				}
			}
		}
		return leafStatusStr;
	}

	public String getStatusStr()
	{
		// 根据状态值获取字符串描述
		ARegionEnum[] regionEnums = ARegionEnum.values();
		for (int i = 0; i < regionEnums.length; i++)
		{
			ARegionEnum regionEnum = regionEnums[i];
			if (regionEnum.toString().startsWith("STATUS_"))
			{
				// 表示是状态的标识
				if (regionEnum.getStatus() == this.getStatus())
				{
					this.statusStr = regionEnum.getName();
					break;
				}
			}
		}
		return statusStr;
	}
}
